<?php
declare (strict_types=1);

namespace app\api\controller;


use app\model\DishesModel;
use app\model\CategoryModel;
use app\model\StaffModel;
use think\facade\Request;
use think\facade\Validate;

class Dishes
{
    //获取菜品列表
    public function list()
    {
        $params = Request::param();
        $params['dishes_status'] = $params['dishes_status'] ?? 0;
        $params['dishes_name'] = $params['dishes_name'] ?? '';

        $validate = Validate::rule([
            'dishes_status|菜品状态' => 'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }
        $model = DishesModel::where('dishes_id', '>', 0);
        if($params['dishes_status']!=0){
            $model->where("dishes_status","=",$params['dishes_status']);
        };
        if($params['dishes_name']){
            $model->where("dishes_name","like",'%'.$params['dishes_name'].'%');
        };
        $dishesList=$model->select();
        foreach ($dishesList as &$value){
            //分类名称
            $categoryOne = CategoryModel::find($value['category_id']);
            $value['category_name'] = $categoryOne['category_name'];
            //是否热销
            if($value['dishes_hot_sell'] == 1){
                $value['dishes_hot_sell_name'] = '是';
            }else{
                $value['dishes_hot_sell_name'] = '否';
            }
            //是否新品
            if($value['dishes_new_product'] == 1){
                $value['dishes_new_product_name'] = '是';
            }else{
                $value['dishes_new_product_name'] = '否';
            }
            //是否推荐
            if($value['dishes_recommend'] == 1){
                $value['dishes_recommend_name'] = '是';
            }else {
                $value['dishes_recommend_name'] = '否';
            }
            //操作员
            $staffOne = StaffModel::find($value['staff_id']);
            $value['username'] = $staffOne['username'];
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'dishesList' => $dishesList
            ],
        ];
        return json($data);
    }
    //根据分类id获取商品列表
    public function lists()
    {
        $params = Request::param();
//        $params['dishes_status'] = $params['dishes_status'] ?? 0;
//        $params['dishes_name'] = $params['dishes_name'] ?? '';
        $validate = Validate::rule([
            'category_id|分类id' => 'require'
        ]);
        if (!$validate->check(['category_id'=>$params['category_id']])) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }
        $model = DishesModel::where('category_id', '=', $params['category_id'])->select();

//        if($params['dishes_status']!=0){
//            $model->where("dishes_status","=",$params['dishes_status']);
//        };
//        if($params['dishes_name']!=0){
////            $where['dishes_name']=array('like','%'.$params['dishes_name'].'%');
////            $model->where($where);
//            $model->where("dishes_name","=",$params['dishes_name']);
//        };
//        $dishesList=$model->select();
//        foreach ($dishesList as &$value){
//            //分类名称
//            $categoryOne = CategoryModel::find($value['category_id']);
//            $value['category_name'] = $categoryOne['category_name'];
//            //是否热销
//            if($value['dishes_hot_sell'] == 1){
//                $value['dishes_hot_sell_name'] = '是';
//            }else{
//                $value['dishes_hot_sell_name'] = '否';
//            }
//            //是否新品
//            if($value['dishes_new_product'] == 1){
//                $value['dishes_new_product_name'] = '是';
//            }else{
//                $value['dishes_new_product_name'] = '否';
//            }
//            //是否推荐
//            if($value['dishes_recommend'] == 1){
//                $value['dishes_recommend_name'] = '是';
//            }else {
//                $value['dishes_recommend_name'] = '否';
//            }
//            //操作员
//            $staffOne = StaffModel::find($value['staff_id']);
//            $value['username'] = $staffOne['username'];
//        }
//        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'dishesList' => $model
            ],
        ];
        return json($data);
    }
    //获取所有菜品列表
    public function listse()
    {
        $params['category_id'] =0;

        $model = DishesModel::select();
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'dishesList' => $model
            ],
        ];
        return json($data);
    }
    public function add()
    {
        // 1. 获取参数
        // 2. 校验参数
        // 3. 验证文本是否合规。
        // 4. 写入数据库
        // 5. 返回数据


        //order增加菜品
        $params = Request::param();
        $validate = Validate::rule([
            'category_id|所属分类' => 'require|between:1,' . PHP_INT_MAX,
            'dishes_name|菜品名称' => 'require',
            'dishes_cover|菜品封面' => 'require',
            'dishes_desc|菜品描述' => 'require',
            'dishes_price|菜品价格' => 'require',
            'dishes_repertory|菜品库存' => 'require',
            'dishes_hot_sell|菜品热销' => 'require',
            'dishes_new_product|菜品新品' => 'require',
            'dishes_recommend|菜品推荐' => 'require',
            'staff_id|员工id' => 'require',
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

//        if (!$params['staff_id']){
//            $params['staff_id']=null;
//        }
//        $params['dishes_status']=1;
        $params['add_time']=$params['update_time']=time();
        $result=DishesModel::create($params);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //菜品删除
    public function delete()
    {
        $dishesId = Request::param("dishes_id");
        $validate = Validate::rule([
            'dishes_id|菜品id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['dishes_id' => $dishesId])) {
            echo $validate->getError();
            exit();
        }
//        $category=DishesModel::find($dishesId);
        $result = DishesModel::destroy($dishesId);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //菜品修改
    public function update()
    {

        $params = Request::param();
        $validate = Validate::rule([
            'dishes_id|菜品id' => 'require|between:1,' . PHP_INT_MAX,
            'category_id|所属分类' => 'require|between:1,' . PHP_INT_MAX,
            'dishes_name|菜品名称' => 'require',
            'dishes_cover|菜品封面' => 'require',
            'dishes_desc|菜品描述' => 'require',
            'dishes_price|菜品价格' => 'require',
            'dishes_hot_sell|菜品热销' => 'require',
            'dishes_repertory|菜品库存' => 'require',
            'dishes_new_product|菜品新品' => 'require',
            'dishes_recommend|菜品推荐' => 'require',
            'staff_id|员工id' => 'require',
            'dishes_status|菜品状态'=>'require'
        ]);
        if (!$validate->check($params)) {
            echo $validate->getError();
            exit();
        }

        $dishes = DishesModel::find($params['dishes_id']);
//
        if ($params['category_id']){
            $dishes['category_id']=$params['category_id'];
        }
        if ($params['dishes_name']){
            $dishes['dishes_name']=$params['dishes_name'];
        }
        if ($params['dishes_cover']){
            $dishes['dishes_cover']=$params['dishes_cover'];
        }
        if ($params['dishes_desc']){
            $dishes['dishes_desc']=$params['dishes_desc'];
        }

        if ($params['dishes_price']){
            $dishes['dishes_price']=$params['dishes_price'];
        }
        if ($params['dishes_hot_sell']){
            $dishes['dishes_hot_sell']=$params['dishes_hot_sell'];
        }
        if ($params['dishes_new_product']){
            $dishes['dishes_new_product']=$params['dishes_new_product'];
        }
        if ($params['dishes_recommend']){
            $dishes['dishes_recommend']=$params['dishes_recommend'];
        }
        if ($params['dishes_status']){
            $dishes['dishes_status']=$params['dishes_status'];
        }
        if ($params['dishes_repertory']){
            $dishes['dishes_repertory']=$params['dishes_repertory'];
        }
        $dishes['update_time'] = time();
        $result = $dishes->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? true : false
            ]
        ];
        return json($data);
    }
}
